# A股股票查询分析器 Makefile
# 提供常用的项目管理命令

# 变量定义
PYTHON = python3
PIP = pip3
PROJECT_NAME = stock-analyzer

# 默认目标
.DEFAULT_GOAL := help

# 帮助信息
help:
	@echo "A股股票查询分析器 - 可用命令:"
	@echo ""
	@echo "  install     - 安装项目依赖"
	@echo "  test        - 运行测试"
	@echo "  run         - 运行主程序"
	@echo "  quick       - 运行快速启动程序"
	@echo "  example     - 运行使用示例"
	@echo "  clean       - 清理缓存文件"
	@echo "  setup       - 初始化项目环境"
	@echo "  check       - 检查代码质量"
	@echo "  format      - 格式化代码"
	@echo "  requirements - 更新依赖文件"
	@echo "  help        - 显示此帮助信息"

# 安装依赖
install:
	@echo "安装项目依赖..."
	$(PYTHON) -m pip install --upgrade pip
	$(PIP) install -r requirements.txt
	@echo "依赖安装完成！"

# 初始化项目环境
setup: install
	@echo "初始化项目环境..."
	$(PYTHON) setup.py
	@echo "项目环境初始化完成！"

# 运行测试
test:
	@echo "运行测试套件..."
	$(PYTHON) test_stock_analyzer.py

# 运行主程序
run:
	@echo "启动A股股票查询分析器..."
	$(PYTHON) stock_analyzer.py

# 运行快速启动程序
quick:
	@echo "启动快速查询..."
	$(PYTHON) quick_start.py

# 运行使用示例
example:
	@echo "运行使用示例..."
	$(PYTHON) example_usage.py

# 清理缓存文件
clean:
	@echo "清理缓存文件..."
	find . -type f -name "*.pyc" -delete
	find . -type d -name "__pycache__" -exec rm -rf {} +
	find . -type d -name "*.egg-info" -exec rm -rf {} +
	rm -rf build/
	rm -rf dist/
	rm -rf .pytest_cache/
	rm -rf .coverage
	rm -rf htmlcov/
	@echo "缓存清理完成！"

# 检查代码质量（如果安装了相关工具）
check:
	@echo "检查代码质量..."
	@if command -v flake8 >/dev/null 2>&1; then \
		echo "运行 flake8 检查..."; \
		flake8 *.py --max-line-length=88 --ignore=E203,W503; \
	else \
		echo "flake8 未安装，跳过代码检查"; \
	fi
	@if command -v pylint >/dev/null 2>&1; then \
		echo "运行 pylint 检查..."; \
		pylint *.py --disable=C0114,C0115,C0116; \
	else \
		echo "pylint 未安装，跳过代码检查"; \
	fi

# 格式化代码（如果安装了相关工具）
format:
	@echo "格式化代码..."
	@if command -v black >/dev/null 2>&1; then \
		echo "使用 black 格式化代码..."; \
		black *.py --line-length=88; \
	else \
		echo "black 未安装，跳过代码格式化"; \
	fi
	@if command -v isort >/dev/null 2>&1; then \
		echo "使用 isort 整理导入..."; \
		isort *.py; \
	else \
		echo "isort 未安装，跳过导入整理"; \
	fi

# 更新依赖文件
requirements:
	@echo "更新依赖文件..."
	$(PIP) freeze > requirements.txt
	@echo "依赖文件已更新！"

# 创建虚拟环境
venv:
	@echo "创建虚拟环境..."
	$(PYTHON) -m venv venv
	@echo "虚拟环境创建完成！"
	@echo "激活虚拟环境: source venv/bin/activate (Linux/Mac) 或 venv\\Scripts\\activate (Windows)"

# 开发模式安装
dev-install:
	@echo "安装开发依赖..."
	$(PIP) install -r requirements.txt
	$(PIP) install flake8 pylint black isort pytest coverage
	@echo "开发依赖安装完成！"

# 运行覆盖率测试
coverage:
	@echo "运行覆盖率测试..."
	@if command -v coverage >/dev/null 2>&1; then \
		coverage run test_stock_analyzer.py; \
		coverage report; \
		coverage html; \
		echo "覆盖率报告已生成到 htmlcov/ 目录"; \
	else \
		echo "coverage 未安装，请先运行 make dev-install"; \
	fi

# 打包项目
package:
	@echo "打包项目..."
	$(PYTHON) setup.py sdist bdist_wheel
	@echo "打包完成，文件在 dist/ 目录中"

# 显示项目信息
info:
	@echo "项目信息:"
	@echo "  名称: $(PROJECT_NAME)"
	@echo "  Python版本: $(shell $(PYTHON) --version)"
	@echo "  pip版本: $(shell $(PIP) --version)"
	@echo "  项目目录: $(shell pwd)"
	@echo "  依赖包数量: $(shell wc -l < requirements.txt)"

# 伪目标声明
.PHONY: help install setup test run quick example clean check format requirements venv dev-install coverage package info 